import type { HttpClientConfig } from 'axios-mapper';
import HttpClient from 'axios-mapper';
import config from '../../config/serviceSettings';

class Https {
  public static manager: HttpClient;
  private constructor() {}

  // eslint-disable-next-line @typescript-eslint/member-ordering
  public static shared(httpConfig: HttpClientConfig) {
    if (!Https.manager) {
      Https.manager = new HttpClient(httpConfig);
    }
    return Https.manager;
  }

  // eslint-disable-next-line @typescript-eslint/member-ordering
  public static updateConfig(httpConfig?: HttpClientConfig) {
    if (Https.manager) {
      Https.manager.httpClient.defaults = Object.assign(Https.manager.httpClient.defaults, httpConfig);
    } else {
      throw new Error('为创建Https实例');
    }
  }
}

export default Https.shared(config);
